package com.example.demo;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnHello;
    private MyButton btnHello2;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnHello = findViewById(R.id.btn_hello);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);

        btnHello2 = findViewById(R.id.btn_hello2);

        //透明度
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"alpha",1,0,1);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();*/

//                ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this,R.animator.set_animator);
//                objectAnimator.setTarget(btnHello);
//                objectAnimator.start();

                //用自定义属性myText实现btnHello2从1变到1000
                /*ObjectAnimator objectAnimator = ObjectAnimator.ofInt(btnHello2,"myText",1,1000,1);
                objectAnimator.setDuration(10000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();*/

                ////组合动画
                // 步骤1：设置需要组合的动画效果
                float curTranslationX = btnHello.getTranslationX();
                ObjectAnimator translation = ObjectAnimator.ofFloat(btnHello, "translationX", curTranslationX, 300,curTranslationX);
                // 平移动画
                ObjectAnimator rotate = ObjectAnimator.ofFloat(btnHello, "rotation", 0f, 360f);
                // 旋转动画
                ObjectAnimator alpha = ObjectAnimator.ofFloat(btnHello, "alpha", 1f, 0f, 1f);
                // 透明度动画

                // 步骤2：创建组合动画的对象
                AnimatorSet animSet = new AnimatorSet();

                // 步骤3：根据需求组合动画
                animSet.play(translation).with(rotate).before(alpha);
                animSet.setDuration(5000);

                // 步骤4：启动动画
                animSet.start();
            }
        });

        //平移
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float curTranslationX = btnHello.getTranslationX();
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello, "translationX",
                        curTranslationX, 600, curTranslationX);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        btnHello.setTranslationY(value);
                        btnHello.setText("Hello" + value);
                    }
                });
                objectAnimator.start();

            }
        });

        //缩放
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"scaleX",
                        1,3,1);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.RESTART);
                objectAnimator.start();
            }
        });

        //旋转
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnHello,"rotation",
                        0,360);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.RESTART);
                objectAnimator.start();
            }
        });
    }
}
